import {
    selectAllMedicine,
    addMedicine,
    updateMedicine,
    deleteMedicine
} from '@/api/medicine'


const getDefaultState = () => {
    return {
    }
}

const state = getDefaultState()

const mutations = {
    RESET_STATE: (state) => {
        Object.assign(state, getDefaultState())
    }
}


const actions = {

    selectAllMedicine({
        commit
    }, medicineInfo) {
        return new Promise((resolve, reject) => {
            const {
                obj,
                page,
                pageSize
            } = medicineInfo
            selectAllMedicine({
                obj: obj,
                page: page,
                pageSize: pageSize
            }).then(response => {
                resolve(response.data)
            }).catch(error => {
                reject(error)
            })
        })
    },

    // use add medicine
    addMedicine({
        commit
    }, medicineInfo) {
        return new Promise((resolve, reject) => {
            const {
                hospitalId,
                name,
                inventory,
                price
            } = medicineInfo
            addMedicine({
                hospitalId: hospitalId,
                name: name,
                inventory: inventory,
                price: price,
            }).then(response => {
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    // use update medicine
    updateMedicine({
        commit
    }, medicineInfo) {
        return new Promise((resolve, reject) => {
            const {
                medicineId,
                hospitalId,
                name,
                inventory,
                price
            } = medicineInfo
            updateMedicine({
                medicineId: medicineId,
                hospitalId: hospitalId,
                name: name,
                inventory: inventory,
                price: price,
            }).then(response => {
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    },

    // use delete medicine
    deleteMedicine({
        commit
    }, ids) {
        return new Promise((resolve, reject) => {
            deleteMedicine(ids).then(response => {
                resolve()
            }).catch(error => {
                reject(error)
            })
        })
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}